package com.hualan.controller;

import com.hualan.pojo.Article;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Result;
import com.hualan.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value="/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping(value="/search")
    public Result getArticles(String title,Integer userId,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult  pageResult = articleService.getArticlesByTitle(title,userId,page, pageSize);
        return Result.success(pageResult);
    }

    @PostMapping(value="/add")
    public Result addArticle(@RequestBody Article article) {
        return articleService.addArticle(article);
    }

    @PutMapping(value="/update")
    public Result updateArticle(@RequestBody Article article) {
        return articleService.updateArticle(article);
    }

    @PutMapping(value="/delete")
    public Result deleteArticles(@RequestBody List<Integer> ids) {
        return articleService.deleteArticles(ids);
    }


    @GetMapping(value="/list")
    public Result getArticlesByUserId(Integer userId,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pageSize)
    {
        PageResult pageResult = articleService.getArticlesByUserId(userId, page, pageSize);
        return Result.success(pageResult);
    }
    @GetMapping("/find")
    public Result find(Integer id){
        return Result.success(articleService.find(id));
    }


}
